/**
 * <copyright>
 *
 * Copyright (c) 2011 Zeligsoft (2009) Limited
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   Zeligsoft (2009) Limited - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.omg.deployment.parts.forms;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider;
import org.eclipse.emf.eef.runtime.api.component.IPropertiesEditionComponent;
import org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.api.parts.IFormPropertiesEditionPart;
import org.eclipse.emf.eef.runtime.impl.notify.PropertiesEditionEvent;
import org.eclipse.emf.eef.runtime.impl.parts.CompositePropertiesEditionPart;
import org.eclipse.emf.eef.runtime.ui.parts.PartComposer;
import org.eclipse.emf.eef.runtime.ui.parts.sequence.BindingCompositionSequence;
import org.eclipse.emf.eef.runtime.ui.parts.sequence.CompositionSequence;
import org.eclipse.emf.eef.runtime.ui.utils.EditingUtils;
import org.eclipse.emf.eef.runtime.ui.widgets.EEFFeatureEditorDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.forms.widgets.Section;
import org.omg.deployment.DeploymentPackage;
import org.omg.deployment.parts.DeploymentViewsRepository;
import org.omg.deployment.parts.PlanLocalityPropertiesEditionPart;
import org.omg.deployment.providers.DeploymentMessages;


/**
 * @author Toby McClean; Zeligsoft (2009) Limited
 * @generated
 */
public class PlanLocalityPropertiesEditionPartForm extends CompositePropertiesEditionPart implements IFormPropertiesEditionPart, PlanLocalityPropertiesEditionPart {

	protected Text constraint;
		protected Button editConstraint;
		private EList constraintList;



	/**
	 * Default constructor
	 * @param editionComponent the {@link IPropertiesEditionComponent} that manage this part
	 * @generated
	 */
	public PlanLocalityPropertiesEditionPartForm(IPropertiesEditionComponent editionComponent) {
    super(editionComponent);
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.parts.IFormPropertiesEditionPart#
	 *  createFigure(org.eclipse.swt.widgets.Composite, org.eclipse.ui.forms.widgets.FormToolkit)
	 * @generated
	 */
	public Composite createFigure(final Composite parent, final FormToolkit widgetFactory) {
    ScrolledForm scrolledForm = widgetFactory.createScrolledForm(parent);
    Form form = scrolledForm.getForm();
    view = form.getBody();
    GridLayout layout = new GridLayout();
    layout.numColumns = 3;
    view.setLayout(layout);
    createControls(widgetFactory, view);
    return scrolledForm;
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.parts.IFormPropertiesEditionPart#
	 *  createControls(org.eclipse.ui.forms.widgets.FormToolkit, org.eclipse.swt.widgets.Composite)
	 * @generated
	 */
	public void createControls(final FormToolkit widgetFactory, Composite view) {
    CompositionSequence planLocalityStep = new BindingCompositionSequence(propertiesEditionComponent);
    planLocalityStep
      .addStep(DeploymentViewsRepository.PlanLocality.Properties.class)
      .addStep(DeploymentViewsRepository.PlanLocality.Properties.constraint);
    
    
    composer = new PartComposer(planLocalityStep) {

      @Override
      public Composite addToPart(Composite parent, Object key) {
        if (key == DeploymentViewsRepository.PlanLocality.Properties.class) {
          return createPropertiesGroup(widgetFactory, parent);
        }
        if (key == DeploymentViewsRepository.PlanLocality.Properties.constraint) {
          return createConstraintMultiValuedEditor(widgetFactory, parent);
        }
        return parent;
      }
    };
    composer.compose(view);
  }
	/**
	 * @generated
	 */
	protected Composite createPropertiesGroup(FormToolkit widgetFactory, final Composite parent) {
    Section propertiesSection = widgetFactory.createSection(parent, Section.TITLE_BAR | Section.TWISTIE | Section.EXPANDED);
    propertiesSection.setText(DeploymentMessages.PlanLocalityPropertiesEditionPart_PropertiesGroupLabel);
    GridData propertiesSectionData = new GridData(GridData.FILL_HORIZONTAL);
    propertiesSectionData.horizontalSpan = 3;
    propertiesSection.setLayoutData(propertiesSectionData);
    Composite propertiesGroup = widgetFactory.createComposite(propertiesSection);
    GridLayout propertiesGroupLayout = new GridLayout();
    propertiesGroupLayout.numColumns = 3;
    propertiesGroup.setLayout(propertiesGroupLayout);
    propertiesSection.setClient(propertiesGroup);
    return propertiesGroup;
  }

	/**
	 * @generated
	 */
	protected Composite createConstraintMultiValuedEditor(FormToolkit widgetFactory, Composite parent) {
    constraint = widgetFactory.createText(parent, "", SWT.READ_ONLY); //$NON-NLS-1$
    constraint.setEnabled(false);
    constraint.setToolTipText(DeploymentMessages.PlanLocality_ReadOnly);
    GridData constraintData = new GridData(GridData.FILL_HORIZONTAL);
    constraintData.horizontalSpan = 2;
    constraint.setLayoutData(constraintData);
    EditingUtils.setID(constraint, DeploymentViewsRepository.PlanLocality.Properties.constraint);
    EditingUtils.setEEFtype(constraint, "eef::MultiValuedEditor::field"); //$NON-NLS-1$
    editConstraint = widgetFactory.createButton(parent, DeploymentMessages.PlanLocalityPropertiesEditionPart_ConstraintLabel, SWT.NONE);
    GridData editConstraintData = new GridData();
    editConstraint.setLayoutData(editConstraintData);
    editConstraint.addSelectionListener(new SelectionAdapter() {

      /**
       * {@inheritDoc}
       * 
       * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
       * @generated
       */
      public void widgetSelected(SelectionEvent e) {
        EEFFeatureEditorDialog dialog = new EEFFeatureEditorDialog(
            constraint.getShell(), "PlanLocality", new AdapterFactoryLabelProvider(adapterFactory), //$NON-NLS-1$
            constraintList, DeploymentPackage.eINSTANCE.getPlanLocality_Constraint().getEType(), null,
            false, true, 
            null, null);
        if (dialog.open() == Window.OK) {
          constraintList = dialog.getResult();
          if (constraintList == null) {
            constraintList = new BasicEList();
          }
          constraint.setText(constraintList.toString());
          propertiesEditionComponent.firePropertiesChanged(new PropertiesEditionEvent(PlanLocalityPropertiesEditionPartForm.this, DeploymentViewsRepository.PlanLocality.Properties.constraint, PropertiesEditionEvent.COMMIT, PropertiesEditionEvent.SET, null, new BasicEList(constraintList)));
          setHasChanged(true);
        }
      }
    });
    EditingUtils.setID(editConstraint, DeploymentViewsRepository.PlanLocality.Properties.constraint);
    EditingUtils.setEEFtype(editConstraint, "eef::MultiValuedEditor::browsebutton"); //$NON-NLS-1$
    return parent;
  }



	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionListener#firePropertiesChanged(org.eclipse.emf.eef.runtime.api.notify.IPropertiesEditionEvent)
	 * @generated
	 */
	public void firePropertiesChanged(IPropertiesEditionEvent event) {
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.PlanLocalityPropertiesEditionPart#getConstraint()
	 * @generated
	 */
	public EList getConstraint() {
    return constraintList;
  }

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.omg.deployment.parts.PlanLocalityPropertiesEditionPart#setConstraint(EList newValue)
	 * @generated
	 */
	public void setConstraint(EList newValue) {
    constraintList = newValue;
    if (newValue != null) {
      constraint.setText(constraintList.toString());
    } else {
      constraint.setText(""); //$NON-NLS-1$
    }
  }

	public void addToConstraint(Object newValue) {
		constraintList.add(newValue);
		if (newValue != null) {
			constraint.setText(constraintList.toString());
		} else {
			constraint.setText(""); //$NON-NLS-1$
		}
	}

	public void removeToConstraint(Object newValue) {
		constraintList.remove(newValue);
		if (newValue != null) {
			constraint.setText(constraintList.toString());
		} else {
			constraint.setText(""); //$NON-NLS-1$
		}
	}




	/**
	 * {@inheritDoc}
	 *
	 * @see org.eclipse.emf.eef.runtime.api.parts.IPropertiesEditionPart#getTitle()
	 * @generated
	 */
	public String getTitle() {
    return DeploymentMessages.PlanLocality_Part_Title;
  }



}
